const RecomCourseModel = require('../db/models/recomCourse');

class RecomCourseService {
    //爬取课程数据
    async addRecomCourse (data) {
        const cid = data.cid;

        const result = await RecomCourseModel.findOne({
            where: { cid }
        })

        if (result) {
            return await RecomCourseModel.update(data, {
                where: { cid }
            })
        } else {
            return await RecomCourseModel.create(data);
        }
    }

    //获取推荐课程数据
    async getRecomCourseData () {
        return await RecomCourseModel.findAll({
            attributes: {
                exclude: ['mainTitle', 'posterUrl','description', 'teacherImg']
            }
        });
    }

    //改变推荐课程上下架状态
    async changeRecomCourseStatus (id, status) {
        const ret = await RecomCourseModel.update({ status }, { 
            where: { cid: id } 
        });

        return ret[0];
    }
}

module.exports = new RecomCourseService();